<?php defined('BASEPATH') OR exit('No direct script access allowed');

// Code here is run before ALL controllers
class MY_Controller extends CI_Controller
{
	function MY_Controller()
	{
		parent::__construct();
		if($this->config->item('debug'))
		{
			$this->output->enable_profiler(TRUE);
		}
		$this->load->database();
		$this->load->helper('dump_variable');
		$this->load->helper('assets');
// 		$this->load->helper('cookie');
		$this->load->helper('pagination');
		$this->load->helper('url');
		$this->load->helper('language');
		$this->load->helper('common');
		$this->load->library('settings_lib');
		$this->load->library('session');
		$this->load->config('language');
	}

	protected function is_ajax()
	{
		return $this->input->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest';
	}
	
	function _reset_validation()
	{
		// Store current rules
		$rules = $this->form_validation->_config_rules;
	
		// Create new validation object
		$this->form_validation = new CI_Form_validation();
	
		// Reset rules
		$this->form_validation->_config_rules = $rules;
	}
	/* function _reset_validation()
	{
		$this->form_validation->_field_data = array();
	    $this->form_validation->_config_rules = array();
	    $this->form_validation->_error_array = array();
	    $this->form_validation->_error_messages = array();
	    $this->form_validation->error_string = '';
	} */
	
	/**
	 * Prepare page data before saving
	 *
	 *
	 */
	function _prepare_data($table)
	{
		// Standard fields
		$fields = $this->db->list_fields($table);
		$data = array();
		$lang_data = array();
		// Set the data to the posted value.
		foreach ($fields as $field)
		{
			$data[$field] = $this->input->post($field);
		}
	
		// Lang data
		$lang_data = array();
	
		$fields = $this->db->list_fields($table. "_lang");
		$supported_languages = $this->config->item('supported_languages');
		
		foreach($supported_languages as $language => $lang_content)
		{
			foreach ($fields as $field)
			{
				$content = $this->input->post($field.'_'. $language);
	
				$lang_data[$language][$field] = $content;
			}
		}
		return array($data, $lang_data);
	}
	
	/**
	 * Checks if the element save process can be done.
	 *
	 * @returns		Boolean		True if the save can be done, false if not
	 *
	 */
	function _check_before_save()
	{
		/* $default_lang = Settings::get_lang('default');
		$default_lang_url = $this->input->post('url_'.$default_lang);
		$default_lang_title = $this->input->post('title_'.$default_lang);
	
		if ($default_lang_url == FALSE && $default_lang_title == FALSE)
		{
			return FALSE;
		} */
	
		return TRUE;
	}
}

/**
 * Returns the CI object.
 *
 * Example: ci()->db->get('table');
 *
 * @staticvar	object	$ci
 * @return		object
 */
function ci()
{
	return get_instance();
}

require_once APPPATH. 'core/Public_Controller.php';
require_once APPPATH. 'core/Admin_Controller.php';